package com.example.musicbox.controller;


import com.example.musicbox.entity.dto.EmailCode;
import com.example.musicbox.entity.dto.LoginDto;
import com.example.musicbox.entity.dto.RegisterDto;
import com.example.musicbox.service.impl.FileOperationServiceImpl;
import com.example.musicbox.service.impl.MUserServiceImpl;
import com.example.musicbox.utils.EmailSender;
import com.example.musicbox.utils.Result;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 帅哥是我
 * @since 2022-08-01
 */
@RestController
@RequestMapping("/m-user")
public class MUserController {

    @Resource
    private EmailSender emailSender;

    @Resource
    private MUserServiceImpl mUserService;

    @Resource
    private FileOperationServiceImpl fileOperationService;


    /**
     * @api {POST} /m-user/getCode 获取用户邮箱验证码
     * @apiVersion 1.0.0
     * @apiGroup 用户控制器
     * @apiName getCode
     * @apiParam (请求参数) {String} email
     * @apiParamExample 请求参数示例
     * email=E2ZHR
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/getCode")
    public Result getCode(@RequestBody @Validated EmailCode code)
    {
        String code1 = emailSender.sendCode(code.getUemail());
        return Result.ok("邮箱验证码为"+code1);
    }


    /**
     * @api {POST} /m-user/userRegister 用户注册
     * @apiVersion 1.0.0
     * @apiGroup 用户控制器
     * @apiName userRegister
     * @apiParam (请求体) {String} username
     * @apiParam (请求体) {String} uemail
     * @apiParam (请求体) {String} upassword
     * @apiParam (请求体) {String} uphone
     * @apiParam (请求体) {String} code
     * @apiParamExample 请求体示例
     * {"upassword":"tzm","code":"cQBe","uphone":"Niqt","uemail":"gr","username":"MIZ1NA25k"}
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/userRegister")
    public Result userRegister(@RequestBody @Validated RegisterDto registerDto)
    {
        Result result = mUserService.userRegister(registerDto);
        return  result;
    }


    /**
     * @api {POST} /m-user/uploadPhoto 上传用户头像
     * @apiVersion 1.0.0
     * @apiGroup 用户控制器
     * @apiName uploadPhoto
     * @apiParam (请求参数) {Object} file
     * @apiParamExample 请求参数示例
     * file=null
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/uploadPhoto")
    public Result uploadPhoto(@RequestParam(value = "file") MultipartFile file)
    {
        Result result = fileOperationService.uploadPhoto(file);
        return result;
    }


    /**
     * @api {POST} /m-user/userLogin 用户登录
     * @apiVersion 1.0.0
     * @apiGroup 用户控制器
     * @apiName userLogin
     * @apiParam (请求体) {String} code
     * @apiParam (请求体) {String} uemail
     * @apiParam (请求体) {String} upassword
     * @apiParam (请求体) {String} uphone
     * @apiParam (请求体) {String} logintype 登录类型凭证
     * @apiParamExample 请求体示例
     * {"upassword":"TTp","code":"9pjMdOhwPs","logintype":"mjrcEh","uphone":"AfCVE","uemail":"UwGW9wY"}
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/userLogin")
    public Result userLogin(@RequestBody LoginDto loginDto)
    {
        Result result = mUserService.userLogin(loginDto);
        return result;
    }


    /**
     * @api {POST} /m-user/uploadFile 文件上传
     * @apiVersion 1.0.0
     * @apiGroup 用户控制器
     * @apiName uploadFile
     * @apiParam (请求参数) {Object} file
     * @apiParam (请求参数) {String} token
     * @apiParamExample 请求参数示例
     * file=null&token=og
     * @apiSuccess (响应结果) {Object} response
     * @apiSuccessExample 响应结果示例
     * {}
     */
    @PostMapping("/uploadFile")
    public Result uploadFile(@RequestParam(value = "file") MultipartFile file, @RequestHeader String token,@RequestParam String type)
    {
        return fileOperationService.uploadFile(file,token,type);
    }






}

